
String.prototype.trim =	function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function search(search){
	form = document.forms[0];
	m = form.action.indexOf("!");
	e = form.action.substring(form.action.indexOf("."));
	if(m != -1) form.action = form.action.substring(0, m) + "!" + search + e;
	form.elements['pager.cpage'].value = '1';
	form.submit();
}

function add(){
	form = document.forms[0];
	form.submit();
}
function deal(oper, id, msg){
    if(msg)
    {
    	if(!confirm(msg))
    	{
    		return;
    	}
    }
	form = document.forms[0];
	m = form.action.indexOf("!");
	e = form.action.substring(form.action.indexOf("."));
	if(m != -1) form.action = form.action.substring(0, m) + "!" + oper + e;
	form.id.value=id;
	form.submit();
	//url = form.action;
	//if(url.indexOf("?")==-1) url = url + "?id="+id;
	//else url = url + "&id="+id;
	//location.href=url;
}
function printToExcel(content,divName,Path){
  if (divName == null) {
    divName = "divExcel";
  }
   if (Path == null) {
    Path = "/commons/pages/";
  }
   
  var frmToPrint = document.createElement("form");
  var hidHtml = document.createElement("input");
  hidHtml.type = "hidden";
  hidHtml.name = "hidHtml";
  if(content!=null)
  {
  	hidHtml.value=content;
  }
  else{
  	hidHtml.value = document.getElementById(divName).innerHTML;
  }
  frmToPrint.appendChild(hidHtml);
  document.body.appendChild(frmToPrint);
  frmToPrint.method = "post";
  frmToPrint.action = Path + "print.jsp";
  frmToPrint.submit();
}


function checkAll(form){
	form = document.forms[0];
	for(var	i=0;i<form.elements.length;i++){
		var	e =	form.elements[i];
		if(e.name != 'chkall' && e.type=='checkbox'	&& e.disabled != true){
			e.checked =	form.chkall.checked;
		}
	}
}

function del(oper,id){
	var	flag = false;
	form = document.forms[0];	
	for	(var i=0;i<form.elements.length;i++){
		var	e =	form.elements[i];
		if(e.name != 'chkall' && e.type=='checkbox'	&& e.checked){
			flag = true;
			break;
		}
	}
	if(flag){		
		if(confirm("删除后数据将不能恢复,确认删除吗？")){
			form.elements['method'].value=oper;
			form.submit();
		}
	} else {
		alert("请至少选择一个删除项！");
		return false;
	}
}


function dealsub(oper,val){
 	form = document.forms[0];
	form.elements['method'].value=oper;
	form.item_code.value=val;
	form.submit();
}

/**
 * 检测数字格式
 * 正确返回(true)，错误返回(false)
 */
function isNumber(sName,tip)
{
	var	obj=document.getElementsByName(sName)[0];
	if(!(/^\d{1,}$/g.test(obj.value)))
	{
		alert(tip+"必须为整数,请你重新填写！");
		obj.focus();
		return false;
	}
	return true;
}

/**
 * 检测数字格式
 * sName 属性名
 * tip 显示名称
 * reg 正则表达式
 * 正确返回(true)，错误返回(false)
 */
function checkNumber(sName,tip,reg)
{
	var	obj=document.getElementsByName(sName)[0];
	if(!(reg.test(obj.value)))
	{
		alert(tip+"数字格式不正确,请你重新填写！");
		obj.focus();
		return false;
	}
	return true;
}

/**
 * 是否为空值
 * 正确返回(true)，错误返回(false)
 */
function isEmpty(sName,tip)
{
	var	obj=document.getElementsByName(sName)[0];
	if(obj.value.trim().length==0)
	{
		alert(tip+"不能为空,请你重新填写！")
		obj.focus();
		return true;
	}
	return false;
}
/**
* select是否选择
* 未选择返回（true），选择返回（false）
*/
function isEmptySelect(sName,tip)
{
	var	obj=document.getElementsByName(sName)[0];
	if(obj.value.trim().length==0)
	{
		alert(tip+"不能为空,请你重新选择！")
		obj.focus();
		return true;
	}
	return false;
}

//获得checkradio的值
function getRadioValue(radio){ 
	var	obj=document.getElementsByName(radio);
	var	ret	= -1;
	for(var	i=0;i<obj.length;i++){
		if(obj[i].checked == true){
			ret	= obj[i].value;
			break;
		}
	}
	return ret;
}

//输入检查
//eg.:onkeypress="return regInput(this,/^[0-9]*$/,String.fromCharCode(event.keyCode))"
function regInput(obj, reg,	inputStr)
{
	var	docSel	= document.selection.createRange()
	if (docSel.parentElement().tagName != "INPUT")	return false
	oSel = docSel.duplicate()
	oSel.text =	""
	var	srcRange	= obj.createTextRange()
	oSel.setEndPoint("StartToStart", srcRange)
	var	str	= oSel.text	+ inputStr + srcRange.text.substr(oSel.text.length)
	return reg.test(str)
}

function isEmail(sName)
{
	var	regex =	/^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/;
	var	obj=document.getElementsByName(sName)[0];
	if(!regex.test(obj.value)){
		alert("电子邮件格式不正确,请你重新填写！!");
		obj.focus();
		return false;
	}

	 return	true;
}

function isUrl(sName)
{
	var	regex =	/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	var	obj=document.getElementsByName(sName)[0];
	if(!regex.test(obj.value)){
		alert("网址格式不正确,请你重新填写！!");
		obj.focus();
		return false;
	}

	 return	true;
}

function isPhone(sName)
{
	var	regex =	/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
	var	obj=document.getElementsByName(sName)[0];
	if(!regex.test(obj.value)){
		alert("电话号码格式不正确,请你重新填写！!");
		obj.focus();
		return false;
	}

	 return	true;
}

function MM_openBrWindow(theURL,winName,features) {	//v2.0
  window.open(theURL,winName,features);
}
/**
* dateDiff原型函数
* @param Date cdate to compare
* @param String interval as a string 
*	y -> year
* 	m -> month
*	w -> weak
*   d -> day (default param)
*/
Date.prototype.dateDiff = function(cdate, interval){
	try{
		cdate.getYear();
	} catch (e) {
		return 0;
	}
	var base = 60 * 60 * 24 * 1000;		//一天的毫秒数
	var result = this - cdate			//Math.abs();
	switch(interval){
		case "y":result /= base * 365;break;
		case "m":result /= base * 365/12;break;
		case "w":result /= base * 7;break;
		default:result /= base;break;
	}
	return(Math.floor(result));
}

/**
* dateDiff原型函数
* @param Date cdate to compare
* @param String interval as a string 
*	y -> year
* 	m -> month
*	w -> weak
*   d -> day (default param)
*/
String.prototype.toDate = function (seperator){
	var numargs = arguments.length;
	if (numargs != 1){
		seperator = "-";
	}
	try{
		var dateArray = this.split(seperator);
		//年份就不考虑了
		if (dateArray[1].charAt(0) == 0){
			dateArray[1] = dateArray[1].charAt(1);
		}
		if (dateArray[2].charAt(0) == 0){
			dateArray[2] = dateArray[2].charAt(1);
		}
		return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]);
	} catch (e) {
		return "undefined";
	}
}

/**
* 结合DOM的函数
* @version build 20051018
* @author jinqinghua@gmail.com
*/

function closeWindow(){
	window.close();
}

/**
 * add options to select
 * @param object targetSelect as html element select
 * @param optionArray as array filled with object Option
 * @version build 20051029
 * @author jinqinghua@gmail.com
 */
function addOptions(targetSelect, optionArray){
	for (i = 0; i < optionArray.length; i++){
		if (optionArray[i] != null) {
		  targetSelect.options[targetSelect.length] = optionArray[i];
		}
	}
}

/**
 * move options from a select to another select
 * @param object targetSelect as html element select
 * @param object targetSelect as html element select
 * @version build 20051018
 * @author jinqinghua@gmail.com
 */
function moveSelected(sourceSelect, targetSelect,isDelete){
	var cachOptionsArray = new Array();
	var index = 0;
	for (var i = sourceSelect.options.length - 1; i >= 0; i--){
		if (sourceSelect.options[i].selected){
			cachOptionsArray[index] = new Option(sourceSelect.options[i].text, sourceSelect.options[i].value);
			if(isDelete==undefined || isDelete==true){
				sourceSelect.options[i] = null;
			}
			index++;
		}
	}
	var exist = false;
	for (var i = cachOptionsArray.length - 1; i >= 0; i--){
		exist = false;
		for (var j = 0; j < targetSelect.options.length; j++){
			if (targetSelect.options[j].value.toString() == cachOptionsArray[i].value.toString()){
				//alert("“" + cachOptionsArray[i].text + "”已经存在了！");
				//sourceSelect.options[sourceSelect.options.length] = cachOptionsArray[i];
				exist = true; 
				break;
			}
		}
		if (!exist){
			targetSelect.options[targetSelect.options.length] = cachOptionsArray[i];
		}
	}
}

/**
 * Select All Options
 * @param object selectElement as html element select
 * @version build 20051029
 * @author jinqinghua@gmail.com
 */
function selectAll(selectElement){
	for (var i = 0; i < selectElement.length; i++){
		selectElement.options[i].selected = true;
	}
}
 
/**
 * 这个函数名命名有点问题，建议用上面的的函数名	
*/
function selectedAll(selectElement){
	selectAll(selectElement);
}

function onlyNum(){

  if ((event.shiftKey)&&(((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))))
	{  
		event.returnValue=false;
	}

 if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
	{ 
	 if(event.keyCode!=8){
		event.returnValue=false;
     }
	}
}
function initProvinceIncAlone(parIndex, obj, selectedValue, subObj, subSelected, level3Obj, level3Selected)
{
	DWRUtil.removeAllOptions(obj);
    BaseDataService.getProvinceList(parIndex, 
    	function(BaseData)
	    {
	    	DWRUtil.addOptions(obj, {'':'请选择'});
	    	DWRUtil.addOptions(obj, BaseData, "p_index", "p_name");
	    	if(selectedValue)
	    	{
		    	DWRUtil.setValue(obj, selectedValue);
		    	if(level3Obj)
		    	{
		    		initProvinceIncAlone(selectedValue, subObj, subSelected, level3Obj, level3Selected);
		    	}
		    	else if(subObj)
		    	{
			    	initProvinceIncAlone(selectedValue, subObj, subSelected);
		    	}
	    	}
	    }
    );
    var o = eval("document.forms[0]."+obj);
    if(o.onchange!=null)
    {
    	o.onchange();
    }
}
function initProvince(parIndex, obj, selectedValue, subObj, subSelected, level3Obj, level3Selected)
{
	DWRUtil.removeAllOptions(obj);
    BaseDataService.getProvinceList(parIndex, 
    	function(BaseData)
	    {
	    	DWRUtil.addOptions(obj, {'':'请选择'});
	    	DWRUtil.addOptions(obj, BaseData, "p_index", "p_name");
	    	if(selectedValue)
	    	{
		    	DWRUtil.setValue(obj, selectedValue);
		    	if(level3Obj)
		    	{
		    		initProvince(selectedValue, subObj, subSelected, level3Obj, level3Selected);
		    	}
		    	else if(subObj)
		    	{
			    	initProvince(selectedValue, subObj, subSelected);
		    	}
	    	}
	    }
    );
    var o = eval("document.forms[0]."+obj);
    if(o.onchange!=null)
    {
    	o.onchange();
    }
}

function initCounty(parIndex, obj, selectedValue, subObj, subSelected,level3Obj, level3Selected)
{
	DWRUtil.removeAllOptions(obj);
    BaseDataService.getProvinceList(parIndex, 
    	function(BaseData)
	    {
	    	DWRUtil.addOptions(obj, {'':'请选择'});
	    	DWRUtil.addOptions(obj, BaseData, "p_index", "p_name");
	    	if(selectedValue)
	    	{
		    	DWRUtil.setValue(obj, selectedValue);
		    	if(subObj)
		    	{
		    		initCity(selectedValue, subObj, subSelected);
		    	}
	    	}
	    }
    );
    var o = eval("document.forms[0]."+obj);
    if(o.onchange!=null)
    {
    	o.onchange();
    }
}

function initCity(parIndex, obj, selectedValue, subObj, subSelected)
{
	DWRUtil.removeAllOptions(obj);
    BaseDataService.getProvinceList(parIndex, 
    	function(BaseData)
	    {
	    	DWRUtil.addOptions(obj, {'':'请选择'});
	    	DWRUtil.addOptions(obj, BaseData, "p_index", "p_name");
	    	if(selectedValue)
	    	{
		    	DWRUtil.setValue(obj, selectedValue);
		    	if(subObj)
		    	{
		    		initCity(selectedValue, subObj, subSelected);
		    	}
	    	}
	    }
    );
    var o = eval("document.forms[0]."+obj);
    if(o.onchange!=null)
    {
    	o.onchange();
    }
}

function initBuildingUnit(buildObj,buildValue,unitObj,unitValue){
	DWRUtil.removeAllOptions(buildObj);
	BaseDataService.getBuildingList(function(BaseData){
		DWRUtil.addOptions(buildObj, {'':'请选择'});
    	DWRUtil.addOptions(buildObj, BaseData, "id", "b_name");
    	if(buildValue){
    		DWRUtil.setValue(buildObj, buildValue);
    		if(unitObj){
    			BaseDataService.getUnitList(buildValue,function(BaseData){
    				DWRUtil.addOptions(unitObj, {'':'请选择'});
    		    	DWRUtil.addOptions(unitObj, BaseData, "id", "unit_name");
    		    	if(unitValue){
    		    		DWRUtil.setValue(unitObj, unitValue);
    		    	}
    			});
    		}
    	}
	});
	
	var o = eval("document.forms[0]."+buildObj);
    if(o.onchange!=null)
    {
    	o.onchange();
    }
}

function initProjAreaBuildingRoom(projObj,projValue,areaObj,areaValue,buildObj,buildValue,roomObj,roomValue){
	DWRUtil.removeAllOptions(projObj);
	BaseDataService.getAllProjList(function(BaseData){
		DWRUtil.addOptions(projObj, {'':'请选择'});
		DWRUtil.addOptions(projObj, BaseData, "ID", "PROJ_NAME");
		if(projValue){
			DWRUtil.setValue(projObj,projValue);
			BaseDataService.selectShareAreaList(projValue,function(BaseData){
				DWRUtil.addOptions(areaObj, {'':'请选择'});
				DWRUtil.addOptions(areaObj, BaseData, "id", "area_name");
				if(areaValue){
					DWRUtil.setValue(areaObj,areaValue);
					BaseDataService.selectBuidlingListByAreaId(areaValue,function(BaseData){
						DWRUtil.addOptions(buildObj,{'':'请选择'});
						DWRUtil.addOptions(buildObj, BaseData, "id", "b_name");
						if(buildValue){
							DWRUtil.setValue(buildObj,buildValue);
							BaseDataService.selectRoomListByBuildId(buildValue,function(BaseData){
								DWRUtil.addOptions(roomObj,{'':'请选择'});
								DWRUtil.addOptions(roomObj, BaseData, "ID", "HOUSE_NUM");
								if(roomValue){
									DWRUtil.setValue(roomObj,roomValue);
								}
							});
						}
					});
				}
			});
		}
	});
	
	var o = eval("document.forms[0]."+projObj);
    if(o.onchange!=null)
    {
    	o.onchange();
    }
}

function initShareList(projValue,areaObj){
	DWRUtil.removeAllOptions(areaObj);
	BaseDataService.selectShareAreaList(projValue,function(BaseData){
		DWRUtil.addOptions(areaObj, {'':'请选择'});
		DWRUtil.addOptions(areaObj, BaseData, "id", "area_name");
	});
}

function initBuildingList(areaValue,buildObj){
	DWRUtil.removeAllOptions(buildObj);
	BaseDataService.selectBuidlingListByAreaId(areaValue,function(BaseData){
		DWRUtil.addOptions(buildObj, {'':'请选择'});
		DWRUtil.addOptions(buildObj, BaseData, "id", "b_name");
	});
}

function initRoomList(buildValue,roomObj){
	DWRUtil.removeAllOptions(roomObj);
	BaseDataService.selectRoomListByBuildId(buildValue,function(BaseData){
		DWRUtil.addOptions(roomObj,{'':'请选择'});
		DWRUtil.addOptions(roomObj, BaseData, "ID", "HOUSE_NUM");
	});
}